<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>product Operator page</title>
</head>
<script type='text/javascript' src='js/prototype.js'></script>
<link rel="stylesheet" type="text/css" href="css/main.css" />
<script type="text/javascript">
	function iteratorTags(){
		var params="max=5&first=0";
		new Ajax.Request("json/iteratorTags", {
			method : "post",
			parameters : params,
			onComplete : function(request) {
				document.getElementById("tagDisplayer").innerHTML = "热门检索标签：";
				var res = request.responseText.evalJSON();
				for(var i=0;null!=res["result"]["product_tagName"+i];i++){
					document.getElementById("tagDisplayer").innerHTML+=
						"<a href='viewOneTag.html?tagNameId="+res["result"]['product_tagNameId'+i]+"'>"
						+res["result"]["product_tagName"+i]
						+"</a>&nbsp;";
				}
			},
			asynchronous : true
		});
	}
	function deleteProduct(productId) {
		var params = "productId=" + productId;
		new Ajax.Request("json/deleteProduct", {
			method : "post",
			parameters : params,
			onComplete : function(request) {
				var res = request.responseText.evalJSON();
				if (res["result"]["flag"] == "true") {
					document.getElementById(productId).parentNode
							.removeChild(document.getElementById(productId));
				}
			},
			asynchronous : false
		});
	}
	function online() {
		var params = "";
		new Ajax.Request("json/online", {
			method : "post",
			parameters : params,
			onComplete : function(request) {
				var res = request.responseText.evalJSON();
				document.getElementById("online").innerHTML = "当前在线人数："
						+ res["result"]["online"];
			},
			asynchronous : true
		});
		iteratorTags();
	}
	function viewProductOutline(productId) {
		var params = "productId=" + productId;
		new Ajax.Request(
				"json/viewProductOutline",
				{
					method : "post",
					parameters : params,
					onComplete : function(request) {
						var res = request.responseText.evalJSON();
						if (res["result"]["flag"] == "true") {
							var image1;
							if (res["result"]["image1"] == null)
								image1 = "image/userPhotoBig.png";
							else
								image1 = res["result"]["image1"];
							var tmp = "<li style='position:relative;height:110px;marigin-top:20px;width:600px;border-bottom:solid' id='"
									+productId
									+"' > <img style='width:100px;height:100px' src='"
									+image1
									+"' /><div style='position:absolute;margin-top:-100px;margin-left:150px;'><a href='viewOneProduct.html?productId="
									+ productId
									+ "'>名称:"
									+ res["result"]["name"]
									+ "</a><br/>价格:"
									+ res["result"]["price"]
									+ "</a><br/>数量:"
									+ res["result"]["amount"]
									+ "<br /><input type='button' value='delete' onclick='deleteProduct("
									+ productId
									+ ")' /><br/><a href='addImage.html?productId="
									+ productId + "'>添加展示图片</a></div></li>";
							$("iteratorProductsTarget").innerHTML += tmp;
						}
					},
					asynchronous : false
				});
	}
	function iteratorProducts() {
		var request = {
			QueryString : function(val) {
				var uri = window.location.search;
				var re = new RegExp("" + val + "=([^&?]*)", "ig");
				return ((uri.match(re)) ? (uri.match(re)[0]
						.substr(val.length + 1)) : null);
			}
		};
		var max = request.QueryString("max");
		var first = request.QueryString("first");
		if (max == null || first == null) {
			max = 5;
			first = 0;
		}

		var params = "max=" + max + "&first=" + first;
		new Ajax.Request(
				"json/iteratorProducts",
				{
					method : "post",
					parameters : params,
					onComplete : function(request) {
						var res = request.responseText.evalJSON();
						if (res["result"]["flag"] == "true") {
							for (var i = 0; res["result"]["productId" + i] != null; i++) {
								viewProductOutline(res["result"]["productId"
										+ i]);
							}

							var amount = res["result"]["amountOfProducts"];
							document.getElementById("amountOfProducts").innerHTML = "共"
									+ amount + "条";
							document.getElementById("firstPage").innerHTML = "<a href='productOwner.html?max="
									+ max + "&first=" + 0 + "' >首页</a>";
							document.getElementById("lastPage").innerHTML = "<a href='productOwner.html?max="
									+ max
									+ "&first="
									+ parseInt(amount - max)
									+ "' >尾页</a>";

							var next = parseInt(first) + parseInt(max) >= parseInt(amount) ? parseInt(amount) - parseInt(max)
									: parseInt(first) + parseInt(max);
							var previous = parseInt(first) - parseInt(max) <= 0 ? 0 : parseInt(first) - parseInt(max);
							document.getElementById("previous").innerHTML = "<a href='productOwner.html?max="
									+ max + "&first=" + previous + "' >上一页</a>&nbsp";
							document.getElementById("next").innerHTML = "<a href='productOwner.html?max="
									+ max + "&first=" + next + "' >下一页</a>&nbsp";

							var initialization = parseInt(first) - 2 * parseInt(max) >= 0 ? parseInt(first)
									- 2* parseInt(max) : 0;
							var end = parseInt(first) + 3 * parseInt(max) <parseInt(amount) ? parseInt(first) + 3
									* parseInt(max) : parseInt(amount)-max;

							for (var i = 0; i <= ((end - initialization) / max) + 1
									&& i <= 4; i++) {
								document.getElementById("pages").innerHTML += "<a href='productOwner.html?max="
										+ max
										+ "&first="
										+ (initialization + (i * max))
										+ "'"
										+ ( parseInt(first) == (parseInt(initialization) + (i * max))? " style='color:red' "
												: "") 
												+ " >" + parseInt((initialization + (i * max))/max +1) + "</a>";
							}
						}
					},
					asynchronous : false
				});
		online();
	}
</script>
<body onload="iteratorProducts()">
	<div class="topbar">
		<ul class="horizon">
			<li style="margin-left: 50%; margin-top: 2px;" class="logo"><a
				href="login.html">首页</a></li>
			<li class="login" id="login">
				<ul class="vertical" style="margin-left: 80px">
					<li class="userInfo"><img src="image/userPhoto.png" /><a>&nbsp;你好，用户</a></li>
					<li class="userInfo2">
						<ul class="horizon">
							<li style="margin-right: 15px;"><img
								src="image/userPhotoBig.png" /></li>
							<li>用户&nbsp;&nbsp;&nbsp;&nbsp;<a>修改密码</a><br />编辑个人资料<br />注销<br /></li>
						</ul>
					</li>
				</ul>
			</li>
		</ul>
	</div>
	<div class="main">
		<ul class="horizon">
			<li>
				<ul class="vertical">
					<li class="managePad">
						<ul class='vertical' style="margin-left:60px;">
							<li><a href="./addProduct.html">add product</a></li>
							<li id="online" style="top:80%;position:absolute"></li>
							<li id="tagDisplayer"></li>
						</ul>
					</li>
				</ul>
			<li class="content" style="position: relative; width: 600px;">
				<ul class="horizon" style="margin-left: 100px">
					<li id="firstPage"></li>
					<li id="previous"></li>
					<li id="pages"></li>
					<li id="next"></li>
					<li id="lastPage"></li>
					<li id="amountOfProducts"></li>
				</ul>
				<ul class="vertical" id="iteratorProductsTarget"
					style="margin-left: 30px; margin-top: 30px">
				</ul>
			</li>
		</ul>
	</div>
</body>
</html>